package com.wangningbo.servlet;

import com.wangningbo.entry.PageBean;
import com.wangningbo.pojo.LinkMan;
import com.wangningbo.service.LinkManService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @Author wangningbo
 * @Date 2021/1/10 11:52
 */

@WebServlet("/linkman")
public class LinkManServlet extends HttpServlet {
    private LinkManService linkManService = new LinkManService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获得method请求参数
        String action = request.getParameter("action");

        //2.判断
//        if("findAll".equals(action)){
//            findAll(request,response);
//        }
//        if("findOne".equals(action)){
//            findOne(request,response);
//        }
//        if("add".equals(action)){
//            add(request,response);
//        }
//        if("update".equals(action)){
//            update(request,response);
//        }
//        if("delete".equals(action)){
//            delete(request,response);
//        }
//        if("findByPage".equals(action)){
//            findByPage(request,response);
//        }
        //利用反射来代替if判断
        try {
            Class aClass = this.getClass();
            Method method = aClass.getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        }catch (Exception e){

        }
    }
    private void findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            Long currentPage = Long.valueOf(request.getParameter("currentPage"));
            Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
            PageBean<LinkMan> pageBean = linkManService.findByPage(currentPage,pageSize);
            request.setAttribute("pageBean",pageBean);
            request.getRequestDispatcher("/list_page.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("分页查询失败");
        }

    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try{
            Integer id = Integer.valueOf(request.getParameter("id"));
            Map<String, String[]> map = request.getParameterMap();
            LinkMan linkMan = new LinkMan();
            BeanUtils.populate(linkMan,map);
            linkManService.updateLinkMan(linkMan,id);
            response.sendRedirect("/linkman?action=findAll");
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("添加失败");
        };
    }

    private void findOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try{
            Integer id = Integer.valueOf(request.getParameter("id"));
            LinkMan linkMan = linkManService.findOne(id);
            request.setAttribute("linkMan",linkMan);
            request.getRequestDispatcher("/update.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("更改失败");
        }
    }

    //删除联系人
    public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        try {
            Integer id = Integer.valueOf(request.getParameter("id"));
            linkManService.deleteLinkManById(id);
            response.sendRedirect("/linkman?action=findAll");
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("删除失败");
        }
    }

    //查询所有的联系人
    public void findAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
       try{
           List<LinkMan> linkManList = linkManService.findAll();
           request.setAttribute("linkManList",linkManList);
           request.getRequestDispatcher("/list.jsp").forward(request,response);
       }catch (Exception e){
           e.printStackTrace();
           response.getWriter().write("查询失败");
       }

    }

    //添加联系人
    public void add(HttpServletRequest request,HttpServletResponse response) throws IOException {
        try{
            Map<String, String[]> map = request.getParameterMap();
            LinkMan linkMan = new LinkMan();
            BeanUtils.populate(linkMan,map);
            linkManService.addLinkMan(linkMan);
            response.sendRedirect("/linkman?action=findAll");
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("添加失败");
        };
    }
}

